﻿using System;
using System.IO;
using System.Windows.Forms;

namespace DBFile.WinApp
{
	public partial class FormMain : Form
	{
		public FormMain()
		{
			InitializeComponent();
		}

		private void btnFile_Click(object sender, EventArgs e)
		{
			OpenFileDialog ofd = new OpenFileDialog();
			if (ofd.ShowDialog() == DialogResult.OK)
			{
				txtFileName.Text = ofd.FileName;
			}

			btnUpload.Enabled = EnableUploadButton();
		}

		private void btnDirectory_Click(object sender, EventArgs e)
		{
			FolderBrowserDialog fbd = new FolderBrowserDialog();
			if (fbd.ShowDialog() == DialogResult.OK)
			{
				txtDirectory.Text = fbd.SelectedPath;
			}

			btnDownload.Enabled = EnableDownloadButton();
		}

		private void btnUpload_Click(object sender, EventArgs e)
		{
			DBFile.Core.DBFile.Move(txtFileName.Text);

			string message = "Error Occurred.";

			// Verify the file exists to message
			if (DBFile.Core.DBFile.Exists(Path.GetFileName(txtFileName.Text)))
			{
				message = "File Uploaded Successfully.";
			}
			MessageBox.Show(message);	
		}

		private void btnRefresh_Click(object sender, EventArgs e)
		{
			var list = DBFile.Core.DBFile.GetFileList(string.Empty);
			this.cboContentList.Items.Clear();

			this.cboContentList.BeginUpdate();
			foreach (var item in list)
			{
				cboContentList.Items.Add(item);
			}
			this.cboContentList.EndUpdate();

			btnDownload.Enabled = EnableDownloadButton();
		}

		private void btnDownload_Click(object sender, EventArgs e)
		{
			string fileName = cboContentList.Items[cboContentList.SelectedIndex].ToString();
			string destPath = Path.Combine(txtDirectory.Text, fileName);
			
			DBFile.Core.DBFile.Move(fileName, destPath);

			string message = "Error Occurred.";
			// Verify the file exists to message
			if (File.Exists(destPath))
			{
				message = "File Downloaded Successfully.";
			}
			MessageBox.Show(message);	
		}

		private bool EnableUploadButton()
		{
			return txtFileName.Text.Length > 0;
		}

        private bool EnableDeleteButton()
        {
            return cboContentList.SelectedIndex > -1;
        }

		private bool EnableDownloadButton()
		{
			return cboContentList.SelectedIndex > -1 && txtDirectory.Text.Length > 0;
		}

		private void txtFileName_Leave(object sender, EventArgs e)
		{
			btnUpload.Enabled = EnableUploadButton();
		}

		private void cboContentList_SelectedIndexChanged(object sender, EventArgs e)
		{
			btnDownload.Enabled = EnableDownloadButton();
            btnDelete.Enabled = EnableDeleteButton();
		}

		private void txtDirectory_Leave(object sender, EventArgs e)
		{
			btnDownload.Enabled = EnableDownloadButton();
		}

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Are you sure you want to delete the file from the database?", "Confirm Deletion", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
                == DialogResult.Yes)
            {
                string fileName = cboContentList.Items[cboContentList.SelectedIndex].ToString();
                
                DBFile.Core.DBFile.Delete(fileName);

                string message = "Error Occurred.";
                
                // Verify the file no longer exists
                if (!DBFile.Core.DBFile.Exists(fileName))
                {
                    message = "File Deleted Successfully.";
                }
                MessageBox.Show(message);	
            }
        }	
		
	}
}
